﻿
@{
    ViewBag.Title = "岗位权限管理";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
}
@model IOA.MES.DataAccess.VModel.VRole

@section Contents{
    <style>
        .layui-tree-entry {
            height: auto;
        }
    </style>
}

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-header">{{lang.Field.Permissions}}</div>
            <div class="layui-card-body">
                <div id="treePower"></div>
            </div>
            <div class="layui-card-footer">
                <button class="layui-btn layui-btn-normal" type="button" id="btnSubmit" style="margin: 20px 20px 20px 200px;"><i class="layui-icon">&#xe605;</i>{{lang.Field.Submit}}</button>
            </div>
        </div>
    </div>
</script>

@section Scripts{
    <script>
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });
        var roleId = parseInt("@Model.PkId");

        setTimeout(function () {
            initLayui();
        }, 50);
        function initLayui() {
            layui.use(['tree', 'layer', 'index'], function () {
                var tree = layui.tree,
                    layer = layui.layer;

                queryTree();
                function queryTree() {
                    layer.load();
                    $.ajax({
                        url: "/Settings/QueryPowers",
                        data: {
                            roleId: roleId
                        },
                        complete: function () {
                            layer.closeAll();
                        },
                        success: function (res) {
                            if (res.ErrCode != 200) {
                                layer.msg(res.ErrMsg, { icon: 6 });
                                return;
                            }

                            var treeData = new Array();
                            treeData.push({
                                id: 0,
                                title: "全部权限",
                                spread: true,
                                children: res.Data
                            });
                            initTree(treeData);
                        }
                    });
                }
                function initTree(treeData) {
                    tree.render({
                        elem: '#treePower'
                        , id: "treePower"
                        , data: treeData
                        , showCheckbox: true
                    });
                }


                $(document).on("click", "#btnSubmit", function () {
                    var checkData = tree.getChecked('treePower');
                    var powerIds = new Array();
                    if (checkData.length > 0) {
                        powerIds = getChildrenIds(checkData[0]);
                    }
                    var loadId = layer.load();
                    $.ajax({
                        async: false,
                        url: "/Settings/SaveRolePowers",
                        data: {
                            roleId: roleId,
                            moduleIds: powerIds
                        },
                        complete: function () {
                            layer.close(loadId);
                        },
                        success: function (res) {
                            if (res.ErrCode != 200) {
                                layer.msg(res.ErrMsg, { icon: 5 });
                                return;
                            }

                            layer.msg("保存成功！", { icon: 6 });
                        }
                    });
                });

                function getChildrenIds(treeModule) {
                    var powerIds = new Array();
                    if (treeModule.id > 0) {
                        powerIds.push(treeModule.id);
                    }
                    for (var i = 0; i < treeModule.children.length; i++) {
                        powerIds = powerIds.concat(getChildrenIds(treeModule.children[i]));
                    }
                    return powerIds;
                }
            });
        }
    </script>
}
